package com.qianbao.qianbaobusiness.ui.support.customermanager;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.CustomerManagerBean;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: Routee
 * @date 2018/6/13
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class CustomerManagerActivity extends BaseMvpActivity<CustomerManagerView, CustomerManagerPresenter> implements CustomerManagerView, SwipeRefreshLayout.OnRefreshListener, BaseRecycleViewAdapter.OnLoadMoreListener {

    @BindView(R.id.tv_title)
    TextView           mTvTitle;
    @BindView(R.id.ll_empty)
    LinearLayout       mLlEmpty;
    @BindView(R.id.rv)
    RecyclerView       mRv;
    @BindView(R.id.srl)
    SwipeRefreshLayout mSrl;
    private String mPos = "";
    private CustomerManagerRvAdapter mRvAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_customer_manager;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("我的客户经理");
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mSrl.setColorSchemeColors(ContextCompat.getColor(this, R.color.text_red));
        mSrl.setOnRefreshListener(this);
        initData();
    }

    private void initData() {
        if (mRvAdapter == null) {
            mRvAdapter = new CustomerManagerRvAdapter(this);
        }
        mRv.setAdapter(mRvAdapter);
        mRvAdapter.setOnLoadMoreListener(this, mRv);
        netManagerList();
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
    }

    @OnClick(R.id.iv_back_toolbar)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        mPos = "";
        netManagerList();
    }

    @Override
    public void onLoadMore() {
        mSrl.setEnabled(false);
        netManagerList();
    }

    private void netManagerList() {
        getPresenter().getManagerList(mPos);
    }

    @Override
    public void getManagerListSuccess(CustomerManagerBean bean) {
        if (TextUtils.isEmpty(mPos)) {
            mRvAdapter.resetData(bean.getItems());
            if (mSrl.isRefreshing()) {
                mSrl.setRefreshing(false);
            }
            if (bean.getItems().size() == 0) {
                mLlEmpty.setVisibility(View.VISIBLE);
            } else {
                mLlEmpty.setVisibility(View.GONE);
                mRv.scrollToPosition(0);
            }
            if (bean.getItems().size() < 20) {
                mRvAdapter.loadMoreEnd(false);
            } else {
                mRvAdapter.loadMoreComplete();
            }
        } else {
            mSrl.setEnabled(true);
            mLlEmpty.setVisibility(View.GONE);
            if (bean.getItems().size() == 0) {
                mRvAdapter.loadMoreEnd(false);
                return;
            } else if (bean.getItems().size() < 20) {
                mRvAdapter.addData(bean.getItems());
                mRvAdapter.loadMoreComplete();
                mRvAdapter.loadMoreEnd(false);
                return;
            }
            mRvAdapter.addData(bean.getItems());
            mRvAdapter.loadMoreComplete();
        }
        mPos = bean.getPos();
    }

    @Override
    public void onNetError() {
        if (mSrl.isRefreshing()) {
            mSrl.setRefreshing(false);
        }
        mSrl.setEnabled(true);
        mRvAdapter.setEnableLoadMore(true);
    }
}
